package ch.common.action.umTableActioin;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import ch.common.dao.BaseDao;
import ch.common.service.umTableService.UMTableService;

/**
 * 
 * @author grc
 * @Date 2016-1-5 13:26:45
 * @param <T>
 * @param <T_Dao>
 */
public class UMTableAction<T,T_Dao extends BaseDao<T>> extends ActionSupport implements IUMTable,ModelDriven<T>
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1256190060637463672L;
	
	// Spring注入
	private UMTableService<T, T_Dao> umTableService;
	// Spring注入
	private T t;
	
	public Integer pageNumber = 1;
	public Integer pageSize = 5;
	
	@Override
	public T getModel() {
		// TODO Auto-generated method stub
		return t;
	}


	@Override
	public String Query() {
		// TODO Auto-generated method stub
		try {
			printWriterToWrite(umTableService.doQuery(pageNumber,pageSize));
		} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
				| InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return IUMTable.Query;
	}

	@Override
	public String Delete() {
		// TODO Auto-generated method stub
		umTableService.doDelete(t);
		return IUMTable.Delete;
	}

	@Override
	public String Save() {
		// TODO Auto-generated method stub
		System.out.println("Save");
		umTableService.doSave(t);
		return IUMTable.Save;
	}
	
	@Override
	public String GetPageCounts() {
		// TODO Auto-generated method stub
		printWriterToWrite(umTableService.doGetPageCounts(pageSize)+"");
		return IUMTable.GetPageCounts;
	}
	
	/**
	 * 改变输出流的内容 <br/>
	 * 改变内容为 msg <br/>
	 * 输出流格式为utf-8<br/>
	 */
	protected void printWriterToWrite(String msg){
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out;
		try {
			out = response.getWriter();
			out.print(msg);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public T getT() {
		return t;
	}
	public void setT(T t) {
		this.t = t;
	}

	public UMTableService getUmTableService() {
		return umTableService;
	}

	public void setUmTableService(UMTableService umTableService) {
		this.umTableService = umTableService;
	}
}
